// pages/login/login.js
/**
 * 登入
 */
let Mcaptcha = require('../../utils/mcaptcha.js');
var comm = require('../../utils/comm_util.js')
var model = require('model/loginModel.js')
var location=require('../componet/location/Location.js')
const app = getApp()
var countdown = 60;
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_show: true
    })
    countdown = 60;
    return;
  } else {
    that.setData({
      is_show: false,
      last_time: countdown
    })
    countdown--;
  }
  setTimeout(function () {
    settime(that)
  }, 1000)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIamgeCode: '1df4',
    last_time: '',
    is_show: true,
    inputPhone:null,
    agreeMent:true,
    shareAk:'',
    showphone:true,
    showimage: true,
    showlogin: false,
    award:''
  },
  
  
  //登入--绑定
  formSubmit: function (e) {
    var that = this
    var errMsg = ''
    var phoneNumber = e.detail.value.phoneNumber
    var phoneCode = e.detail.value.phoneCode
    console.log("phoneNumber=", phoneNumber, ";phoneCode=", phoneCode)
    if (!comm.validationPhone(phoneNumber)) {
      errMsg = "请输入正确的手机号！"
    } else if (comm.isEmpty(phoneCode) || comm.containNumberAndChar(phoneCode)) {//不为空，没有汉字
      errMsg = "请输入正确的短信验证码！"
    }
    if(!this.data.agreeMent){
      errMsg = "请输入勾选用户隐私协议！"
    }
    if (errMsg.length > 0) {
      wx.showToast({
        title: errMsg,
        icon: 'none',
      })
      return
    }
    model.requestLogin(phoneNumber,phoneCode,this.data.shareAk,function(data){
      // var _openId = data.openId
      app.globalData.userInfo = data
      // app.globalData.openId = _openId
      var _tmpUserInfo = {}
      for (var prop in data) {
        if (prop != 'openId') {
          _tmpUserInfo[prop] = data[prop];
        }
      }
      wx.setStorage({
        key: 'user',
        data: _tmpUserInfo,
      })
      wx.switchTab({
        url: '../index/index',
      })
    })
  },
  seeAgreement:function(e){
    wx.navigateTo({
      url: '../web/html?url=https://web.fenbibox.com/chalkdoc/privacyProtocol.html&title=用户隐私协议',
    })
  },
  wxLogin: function (e) {
    var that = this;
    that.setData({
      showwx:true,     //展示回复框
      showphone:false
    })
  },
  phoneLogin: function (e) {
    var that = this;
    that.setData({
      showwx: false,     //展示回复框
      showphone: true
    })
  },
  showLogin: function (e) {
    var that = this;
    that.setData({   //展示回复框
      showimage: false,
      showlogin: true
    })
  },
  checkboxChange: function (e) {
    if (this.data.agreeMent) {
      this.setData({
        agreeMent: false
      })
    } else {
      this.setData({
        agreeMent: true
      })
    }
  },
  listenerPhoneInput: function (e) {
    this.data.inputPhone = e.detail.value;
  },
  getCode: function (e) {
    if (!comm.validationPhone(this.data.inputPhone)){
      wx.showToast({
        title: "请输入正确的手机号！",
        icon: 'none',
      })
      return
    }
    var that = this;
    // 将获取验证码按钮隐藏60s，60s后再次显示
    that.setData({
      is_show: (!that.data.is_show)  //false
    })
    settime(that);
    model.getCode(this.data.inputPhone,function(data){
      wx.showToast({
        title: "短信已发送，请注意查收",
        icon: 'none',
      })
    })
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if ("shareAk" in options){
      var _shareAk = options.shareAk;
      var _award = options.award;
      this.showModal()
      if (_shareAk != null && typeof (_shareAk) != undefined) {
        this.setData({
          shareAk: _shareAk,
          award: _award
        })
      }
    }
    
 


  },
  //微信登录
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      wx.login({
        success: function (res) {
          //插入登录的用户的相关信息到数据库
          model.requestwx("xcx", res.code, function (data) {
            var _openId = data.openId
            app.globalData.userInfo = data
            app.globalData.openId = _openId
            var _tmpUserInfo = {}
            for (var prop in data) {
              if (prop != 'openId') {
                _tmpUserInfo[prop] = data[prop];
              }
            }
            wx.setStorage({
              key: 'user',
              data: _tmpUserInfo,
            })
            wx.switchTab({
              url: '../index/index',
            })
          })
        }
      });

    }
    else
    {
      wx.showToast({
        title: "您点击了拒绝授权，将无法进入小程序，请授权之后再进入",
        icon: 'none',
      })
      return
    }
  },
  showModal() {
    this.setData({
      _showHongBaoDialog: true
    })
  },
  hideModal(e) {
    this.setData({
      _showHongBaoDialog: false
    })
  },
  closeHongBaoDialog:function(event){
    this.hideModal()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    location.getLocation(function(data){
      if(data!=null){
        console.log("get location success")
        getApp().globalData.userLocationInfo=data
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})